package cn.akwangl.design.pattern.behavioral.mediator.v2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 抽象同事类
 *
 * @author youyongkun
 * @date 2021/12/7 23:12
 */
public abstract class Customer extends JFrame implements ActionListener {

    /**
     * 中介者
     */
    protected Medium medium;

    protected String name;

    protected JTextField sentText;
    protected JTextArea ReceiveArea;

    public Customer(String name) {
        super(name);
        this.name = name;
    }

    public void setMedium(Medium medium) {
        this.medium = medium;
    }

    @Override
    public String getName() {
        return name;
    }

    void ClientWindow(int x, int y) {
        Container cp;
        JScrollPane sp;
        JPanel p1, p2;
        cp = this.getContentPane();
        sentText = new JTextField(18);
        ReceiveArea = new JTextArea(10, 18);
        ReceiveArea.setEditable(false);
        p1 = new JPanel();
        p1.setBorder(BorderFactory.createTitledBorder("接收内容："));
        p1.add(ReceiveArea);
        sp = new JScrollPane(p1);
        cp.add(sp, BorderLayout.NORTH);
        p2 = new JPanel();
        p2.setBorder(BorderFactory.createTitledBorder("发送内容："));
        p2.add(sentText);
        cp.add(p2, BorderLayout.SOUTH);
        sentText.addActionListener(this);
        this.setLocation(x, y);
        this.setSize(250, 330);
        //窗口大小不可调整
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }


    /**
     * 发送消息
     *
     * @param ad 消息
     * @author youyongkun
     * @date 2021/12/7 23:18
     */
    public abstract void send(String ad);

    /**
     * 接收消息
     *
     * @param from 消息
     * @param ad   消息
     * @author youyongkun
     * @date 2021/12/7 23:18
     */
    public abstract void receive(String from, String ad);

    @Override
    public void actionPerformed(ActionEvent e) {
        String tempInfo = sentText.getText().trim();
        sentText.setText("");
        this.send(tempInfo);
    }
}
